package ufro.biblioteca.ws.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ufro.biblioteca.Socio;
import ufro.biblioteca.ws.vologin.VOLogin;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/loginWeb.jr")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public LoginServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String usuario = request.getParameter("usuario");
		String password = request.getParameter("password");
		System.out.println("Usuario : "+usuario);
		System.out.println("Password : "+password);
		
		if(usuario.equals("Peter") && password.equals("1234")) {
			System.out.println("Welcome . . .");
			
			VOLogin vo = new VOLogin();
			vo.setUser(usuario);
			vo.setPassword(password);
			vo.setNombre("Pedro Manriquez");
			
			HttpSession session = request.getSession(true);
			List<Socio> lista = new ArrayList<Socio>();
			
			session.setAttribute("usuarioLogeado", vo);
			session.setAttribute("listaSocio", lista);
			request.getRequestDispatcher("welcome.jsp").forward(request, response);
			System.out.println("LOGIN");
		} else {
			System.out.println("Error en credenciales de acceso . . .");
			response.sendRedirect("login.html");
		}
	}
}
